# coding=utf8
from __future__ import unicode_literals
'''


@author: Soul
'''

from lxml import *
import lxml.html
import urllib2
from lxml.html import fromstring
from lxml.html import HtmlElement

def getjarinfo(url):
    proxy_needed = False
    if proxy_needed:
        proxy_handler = urllib2.ProxyHandler({'http' : 'http://cn-proxy.jp.oracle.com:80'})
        opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
        urllib2.install_opener(opener)
    
    c = None
    try:
        headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
        req = urllib2.Request(url=url, headers=headers)
        c = urllib2.urlopen(req)
    except Exception as e:
        print e
        
    if not c == None:
        f = c.read()
        f = f.decode('utf8')
        f = f.lstrip("\r\n")
        if f.find('<!DOCTYPE') != -1:
            lines = f.split('\n')
            rm_head_lines = lines[1:(len(lines) -1)]
            f = ''.join(rm_head_lines)

        pasred_html = fromstring(f)
        #
        # Replace the xpath here to validate your xpath value
        #
        x_content = pasred_html.xpath("//div[@class='job_wrap']/div[@class='left']/div[@class='job_des']")
        if isinstance(x_content, list):
            print len(x_content)
            if len(x_content) > 0:
                x_content = x_content[0]
            else:
                x_content = ''

        value = ''
        if isinstance(x_content, HtmlElement):
            value = x_content.text_content()
        else:
            value = x_content                                                
        
        # removelist = ['\\xa0', '\\r', '\\n', ' ', '\\t', '\\a', '\\b', '\\f', '\\v', '\\0']
        # import string
        # if isinstance(value, unicode):
        #     try:
        #         t = (unicode)(value.encode('UTF-8'))
        #         for rl in removelist:
        #             t = t.replace(rl, '')
        #         value = t
        #     except:
        #         value = value.encode('raw_unicode_escape')

        print value
    
if __name__ == '__main__':
    #
    # Replace the url to the page URL you want to test
    #
    url = 'http://www.allzhaopin.com/zhiwei/job10143376.html?utm_source=zhiwei&utm_medium=baidu&utm_campaign=ald'
    getjarinfo(url)
